{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "UpdateDownTool\n",
      "windows虚拟键盘控制.ipynb - stock - Visual Studio Code\n",
      "大智慧\n",
      "大智慧-医疗ETF\n",
      "掘金量化终端\n",
      "微信\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "Microsoft Text Input Application\n",
      "图片查看\n",
      "DeepSeek 和另外 2 个页面 - 用户配置 1 - Microsoft​ Edge\n",
      "Program Manager\n"
     ]
    }
   ],
   "source": [
    "import pygetwindow as gw\n",
    "\n",
    "# 获取所有窗口的标题\n",
    "window_titles = gw.getAllTitles()\n",
    "\n",
    "# 打印所有窗口的标题\n",
    "for title in window_titles:\n",
    "    print(title)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "PyGetWindowException",
     "evalue": "Error code from Windows: 0 - 操作成功完成。",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mPyGetWindowException\u001b[0m                      Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_1536\\1123717122.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     11\u001b[0m     \u001b[0mwindow\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpyautogui\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetWindowsWithTitle\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwindow_title\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 12\u001b[1;33m     \u001b[0mwindow\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mactivate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     13\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mIndexError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     14\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mf\"无法找到标题为 '{window_title}' 的窗口\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\ProgramData\\Anaconda3\\lib\\site-packages\\pygetwindow\\_pygetwindow_win.py\u001b[0m in \u001b[0;36mactivate\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    244\u001b[0m         \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mctypes\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwindll\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0muser32\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSetForegroundWindow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_hWnd\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    245\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mresult\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 246\u001b[1;33m             \u001b[0m_raiseWithLastError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    247\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    248\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\ProgramData\\Anaconda3\\lib\\site-packages\\pygetwindow\\_pygetwindow_win.py\u001b[0m in \u001b[0;36m_raiseWithLastError\u001b[1;34m()\u001b[0m\n\u001b[0;32m     97\u001b[0m     information from GetLastError() and FormatMessage().\"\"\"\n\u001b[0;32m     98\u001b[0m     \u001b[0merrorCode\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mctypes\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwindll\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mkernel32\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGetLastError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 99\u001b[1;33m     \u001b[1;32mraise\u001b[0m \u001b[0mPyGetWindowException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Error code from Windows: %s - %s'\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0merrorCode\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_formatMessage\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0merrorCode\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    100\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    101\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mPyGetWindowException\u001b[0m: Error code from Windows: 0 - 操作成功完成。"
     ]
    }
   ],
   "source": [
    "import pyautogui\n",
    "import time\n",
    "import pygetwindow as gw\n",
    "\n",
    "# 通达信软件的窗口标题\n",
    "window_title = \"中信建投网上交易卓越gm版V10.04 - [版面-竖屏2]\"\n",
    "# 等待几秒钟确保通达信软件已经打开\n",
    "# time.sleep(5)\n",
    "# 查找通达信软件的窗口并将其激活\n",
    "try:\n",
    "    window = pyautogui.getWindowsWithTitle(window_title)[0]\n",
    "    window.activate()\n",
    "except IndexError:\n",
    "    print(f\"无法找到标题为 '{window_title}' 的窗口\")\n",
    "    exit()\n",
    "\n",
    "# 等待几秒钟确保窗口已经激活\n",
    "# time.sleep(2)\n",
    "\n",
    "# 输入股票代码\n",
    "pyautogui.write('600600', interval=0.25)\n",
    "\n",
    "# 按下回车键\n",
    "pyautogui.press('enter')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
